$(document).ready(function(){
	$("#submit").click(function() {
		$('#user_name').trigger('blur');
		$('#user_phone').trigger('blur');
		$('#password').trigger('blur');
		$('#password_repeat').trigger('blur');
		if($('.onerror').length > 0){
			return false;
		}else{
			getajax();
		}
	});

	function getajax() {
		var $user_name = $('#user_name').val();
		var $user_phone = $('#user_phone').val();
		var $user_email = $('#password').val();
		var $password_repeat = $('#password_repeat').val();
		var data = {
			'user_name': $user_name,
			'user_phone': $user_phone,
			'password': $user_email,
			'password_repeat': $password_repeat,
		};

		$.ajax({
			type: "post",
			url: publicUrl+'login/register',
			data: data,
			jsonp: "callback",
			dataType: "jsonp",
			async: false,
			success: function(d) {
				console.log(d);
				if (d.status == 1) {
					alert("注册成功！请前往登录");
					window.location.href = "index.html";
				} else if (d.status == 2) {
					alert("注册失败！请检查信息是否填写无误");
				} else if (d.status == 0) {
					alert("该手机号已经注册了，请不要重复注册！！");
				}
			}
		});
	}
	
	$('.input_text').blur(function(){
		$(this).parent().find('.blur').remove();
		var $text = '';
		var $class = '';
		if ($(this).is('#user_name')) {
			if($(this).val() == ''){
				$class = 'onerror';
				$text = '姓名不能为空！！';
			}
		}else if($(this).is('#user_phone')){
			if($(this).val() == ''){
				$class = 'onerror';
				$text = '手机号不能为空！！';
			}else if(!$(this).val().match(/^1[3|4|5|7|8]\d{9}$/)){
				$class = 'onerror';
				$text = '该手机号不合法！';
			}
		}else if($(this).is('#password')){
			if($(this).val() == ''){
				$class = 'onerror';
				$text = '密码不能为空！！';
			}else if(!$(this).val().match(/^\d{6,16}$/)){
				$class = 'onerror';
				$text = '请输入长度6-16位字母数字组合！';
			}
		}else if($(this).is('#password_repeat')){
			if($(this).val() == ''){
				$class = 'onerror';
				$text = '密码不能为空！！';
			}else if($(this).val() != $('#password').val()){
				$class = 'onerror';
				$text = '两次输入密码不一致！！';
			}
		}
		$(this).parent().append('<span class="blur '+$class+'">'+$text+'</span>');
	});
	
});
